package edu.scau.cxq.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.cxq.gp.entity.Inventory;
import edu.scau.cxq.gp.entity.RecordOut;
import edu.scau.cxq.gp.entity.Warehouse;
import edu.scau.cxq.gp.mapper.InventoryMapper;
import edu.scau.cxq.gp.mapper.RecordOutMapper;
import edu.scau.cxq.gp.mapper.WarehouseMapper;
import edu.scau.cxq.gp.service.IRecordOutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.scau.cxq.gp.vo.RecordOutVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 存储出库信息 服务实现类
 * </p>
 *
 * @author CXQ
 * @since 2022-01-30
 */
@Service
public class RecordOutServiceImpl extends ServiceImpl<RecordOutMapper, RecordOut> implements IRecordOutService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private RecordOutMapper recordOutMapper;


    /**
     * 出库之前的操作：包括减库存， 改变仓库空闲货架数
     */
    @Override
    public boolean beforeRecordOut(RecordOut recordOut) {

        // 客户需要的货物数量
        Integer needs = recordOut.getOutCount();

        // 选择最便宜的一批货卖掉 以获取最大利益
        Inventory leastInventory = inventoryMapper.getLeastInventory(recordOut.getGoodsId(), needs);

        if (null == leastInventory) {
            return false;
        }

        recordOut.setWareId(leastInventory.getWareId());
        recordOut.setInPrice(leastInventory.getInGoodsPrice());

        // 从库存中取货 减库存
        leastInventory.setInGoodsCount(leastInventory.getInGoodsCount() - needs);

        int amount = (int) Math.ceil((double) leastInventory.getInGoodsCount() / leastInventory.getInGoodsCost());
        // 计算出空闲出来的货架数
        int free = leastInventory.getInGoodsAmount() - amount;
        leastInventory.setInGoodsAmount(amount);

        // 减库存
        inventoryMapper.updateById(leastInventory);

        // 改变仓库空闲货架数
        Warehouse warehouse = warehouseMapper.selectById(leastInventory.getWareId());
        warehouse.setWareFree(warehouse.getWareFree() + free);
        warehouseMapper.updateById(warehouse);

        return true;
    }

    @Override
    public PageInfo<RecordOutVO> getRecordOutVOList(Integer currentPage, Integer size, Long goodsId, Long cusId, Long wareId) {
        PageHelper.startPage(currentPage, size);
        List<RecordOutVO> recordOutVOS = recordOutMapper.selectRecordOutVOList(goodsId, cusId, wareId);
        PageInfo<RecordOutVO> recordOutVOPageInfo = new PageInfo<>(recordOutVOS);
        return recordOutVOPageInfo;
    }

    /**
     * 撤销出库
     *
     * @param recordOutId
     * @return
     */
    @Override
    public boolean rollbackRecordOut(Long recordOutId) {
        // 删除出库记录
        RecordOut recordOut = recordOutMapper.selectById(recordOutId);
        recordOutMapper.deleteById(recordOutId);

        // 加库存
        QueryWrapper<Inventory> inventoryQueryWrapper = new QueryWrapper<>();
        inventoryQueryWrapper.eq("goods_id", recordOut.getGoodsId());
        inventoryQueryWrapper.eq("ware_id", recordOut.getWareId());
        inventoryQueryWrapper.eq("in_goods_price", recordOut.getInPrice());

        Inventory inventory = inventoryMapper.selectOne(inventoryQueryWrapper);

        if (inventory == null) {
            return false;
        }

        Integer inGoodsAmount = inventory.getInGoodsAmount(); // 该批库存消耗的货架数
        Integer inGoodsCount = inventory.getInGoodsCount(); // 该批库存货物总数
        Integer outCount = recordOut.getOutCount();
        Integer count = inGoodsCount + outCount;
        Integer inGoodsCost = inventory.getInGoodsCost(); // 该货物一个货架能装的数量
        int newAmount = (int) Math.ceil((double) count / inGoodsCost);
        inventory.setInGoodsAmount(newAmount);
        inventory.setInGoodsCount(count);

        // 更新库存
        inventoryMapper.updateById(inventory);

        int newCosts = newAmount - inGoodsAmount;

        // 修改仓库空闲货架数
        Warehouse warehouse = warehouseMapper.selectById(recordOut.getWareId());
        warehouse.setWareFree(warehouse.getWareFree() - newCosts);

        warehouseMapper.updateById(warehouse);

        return true;
    }
}
